package common.web.vo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import common.hibernate.vo.Page;

public class Result {

	public static Result SUCCESS = new Result(true, "");

	public static Result createPageResult(Page<?> page) {
		Result result = new Result();
		result.setPage(page);
		result.success = true;
		return result;
	}

	public static Result createResult(List<? extends Object> data) {
		return new Result(data);
	}

	public static Result createResult(Object entity) {
		return new Result(entity);
	}

	public static Result createResult(Object entity, List<? extends Object> data) {
		return new Result(entity, data);
	}

	public static Result fail(String msg) {
		return new Result(false, msg);
	}

	public static Result fail(Status status) {
		return new Result(status.getValue());
	}

	public enum Status {
		/**
		 * 未鉴权，需要重新登录
		 */
		UNAUTHORIZED(401),

		/**
		 * 禁止访问
		 */
		DENIED(403),

		/**
		 * 会话超时，需要重新登录
		 */
		SESSIONTIMEOUT(600);

		private int value = 200;

		Status(int value) {
			this.value = value;
		}
		
		public int getValue(){
			return this.value;
		}
	}

	private List<? extends Object> data;

	private Object entity;

	private Map<String, Object> map = new HashMap<String, Object>();
	private String msg;

	private Page<?> page;

	private boolean success;

	private int status = 200;

	private <T> Result() {

	}

	public Result(boolean success, String msg) {
		this.success = success;
		this.msg = msg;
	}

	public <T> Result(List<T> data2) {
		this.success = true;
		this.data = data2;
	}

	public Result(int status) {
		this.status = status;
	}

	public Result(Object entity) {
		this.success = true;
		this.entity = entity;
	}

	public Result(Object entity, List<? extends Object> data) {
		this.data = data;
		this.entity = entity;
		this.success = true;
	}

	public void addMap(String key, Object value) {
		this.map.put(key, value);
	}

	public List<? extends Object> getData() {
		return data;
	}

	public Object getEntity() {
		return entity;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public String getMsg() {
		return msg;
	}

	public Page<?> getPage() {
		return page;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setData(List<Object> data) {
		this.data = data;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setPage(Page<?> page) {
		this.page = page;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getStatus() {
		return status;
	}

}
